Java Coding Conventions
Outline

Purpose

The life of most code in is maintenance therefore it is good to have conventions.

The conventions are not intended to be too strict.

The conventions are based on Sun’s recommendations and are what most books use.

Naming Conventions

Naming files with extensions, packages, classes, interfaces, methods, variables and constants.

File Suffixes

Java source is .java

Java "binary" bytecode file is .class

Common File Names

Application compilation make file

Readme

Packages

A collection of classes that work together to provide some kind of service. Generally used as utility services to an application.

The unique naming convention is so that the packages if distributed to the world will have a unique name.

Classes

Nouns, first letter of each word capitalized.

Interfaces

Nouns, first letter of each word capitalized.

Methods

Verbs, first letter is lower case, other internal words first letter is capitalized.

Variables

Variables are in mixed case with a lowercase first letter. Internal words start with capital letters.

Constants

All letters are capitalized.

Source Code Structure

This has two major parts:

  1. Overall structure of items in a file.
  2. Structure of code statements.

Source File Overall Structure

Keep files small.

Three major sections:

  1. Beginning Comments
  2. Package and Import Statements
  3. Class or Interface Declaration

Beginning Comments

Information about class, author, version, revisions, copyrights.

Package and Import Statements

All packages and imports if any that are needed.

Class or Interface Declaration

The public class or interface declared first, then other types.

Each class or interface has seven section to it:

  1. Class/Interface documentation comments
  2. Class or Interface statements
  3. Class/Interface implementation comments
  4. Class (static) variables
  5. Instance variables
  6. Constructors
  7. Methods

Class/Interface documentation comments

Documentation that describes the class/interface as in accordance with the format used by javadoc.

Class or Interface statement

Java source code statements that declare the class or interface.

Class/Interface implementation comments

Comments about the implementation of the class. These comments are not to be revealed to the user (could be another programmer) of the class. These comments describe the algorithmic nature (the why of the code) of the class. Do NOT write comments that are redundant of what the code does.

Class (static) variables

The variables that are class wide and exist even before the object is instantiated.

Instance variables

The variables that are class wide and exist after the object is instantiated.

Constructors

All the constructors of the object. Always code a default constructor. Don’t allow the Java environment implicitly provide one.

Methods

Group methods within the class by functionality, not by access modifier (public, private, etc…).

Methods have a structure within themselves. Documentation (javadoc) comments go before the method declaration. Implementation comments go after the method declaration. Then method variables and then code logic statements. The next section Source Statement Structure provides more information about this structure.

Source Statement Structure

Format source code statements for readability and the avoidance of errors because of implied syntax or dangling characters.

Statements per Line

One statement per line, even for variable declarations of the same type.

Line Length

Less than 70 characters is best. Line wrapping is covered below.

Indentation and White Space

Use indentation to make code readable but be careful about the indentation conveying meaning of logic flow that is not correct. This is most likely to happen with loops and conditionals.

Use whitespace to distinguish between keywords and methods.

Line Wrapping

If long lines can’t be avoided to format them for readability.

Variable Initialization and Placement

Put variables at beginning of class/interface and methods.

Don’t use in-line variable declaration.

Even though not always required by Java, initialize variables; aids in understandability.

Class, Interface and Method Declarations

No space after method name and ( of the argument list.

Align { and } on separate line on the left.

Some will NOT like this, they prefer the dangling { at end of statements.

Dangling is the more widely accepted.

I find it leads to errors when reading and updating code.

Use whatever method you prefer.

Compound Statements

Same { } format as above.

Return Statements

Don’t need to use ( ) unless it makes the value more obvious.

if, if-else, if else-if else Statements

Same { } format as above.

Use good indentation.

Use separate lines.

Use explicit coding, basically the if, if-else format.

for Statements

Same { } format as above.

Use good indentation.

while Statements

Same { } format as above.

Use good indentation.

do-while Statements

Same { } format as above.

Use good indentation.

switch Statements

Same { } format as above.

Use good indentation.

Indicate if fall-through logic is to occur.

Always code a default case.

try-catch Statements

Same { } format as above.

Use good indentation.

Use separate lines.

Programming Practices

Good coding techniques

Providing Access to Instance and Class Variables

Provide access to variables via methods.

Referring to Class Variables and Methods

Constants

Don’t hardcode anything.

Variable Assignments

One statement per line rule.

Don’t use embedded assignments.

Miscellaneous Practices

Parentheses

Explicit code rule. Don’t assume others know the precedence rules.

Returning Values

Learn to use the "?" conditional operator

Expressions before "?" in the Conditional Operator

Learn to use the "?" conditional operator

Special Comments

Dealing with bogus/broken code.

Miscellaneous

Explicit code rule.

Portability Conventions

Portability and 100% Pure Java

Documentation

Source Code Comments

Documentation (javadoc) comments and Implementation comments

Implementation Comments

Documents how, why, algorithm of the code. Indented for the programmer who maintains the code, not for the programmer who uses the code.

Block Comments

Align on the *

Single Line Comments

Trailing Comments

End-of-Line Comments

Documentation Comments

Documents the public interface to the object. Indented for the programmer who uses the class so that they know what methods are available, the parameters required and what the method does and returns.

Source Code Format Example